<?php

define("FIFO_IREXEC", "/tmp/irexec.fifo");
define("FIFO_PERTD2", "/tmp/pertd2.fifo");

define("LOG_ERROR", 1);
define("LOG_INFO", 2);
define("LOG_DEBUG", 3);

define("PAGER_SIZE", 4);

define("NL", "\n");
define("EOD", "==EOD==");

//                   12345678901234567890
define("CONFIRM_1", "Are you sure you");
define("CONFIRM_2", "want to do this?");
define("CONFIRM_3", "");
define("CONFIRM_4", "Press OK to Proceed");

// INITIALISATION

define("LOG_LEVEL", LOG_DEBUG);

// MENUS

$config_file = "menu.xml";
$config = simplexml_load_file($config_file);

$xml_menus = $config->menus;

$menus = array();

foreach($xml_menus->menu as $xml_menu) {
	// Add Items
	$items = array();
	foreach($xml_menu->item as $xml_item) {
		array_push($items, array("label" => $xml_item["label"] . "", "menu" => $xml_item["menu"] . "", "command" => $xml_item["command"] . "", "confirm" => $xml_item["confirm"] . ""));
	}
	// Add Menu
	$menu = array("id" => $xml_menu["id"] . "", "parent" => $xml_menu["parent"] . "", "items" => $items);
	array_push($menus, $menu);
}

// STARTUP

clear_lcd();

$page = 1;

// MAIN LOOP

logger("Started");
logger("Waiting for LIRC events");

$menu = $menus[0];

logger("Setting default menu");

while(true) {

	refresh_lcd(render_menu($menu, $page));

	$pages = ceil(count($menu["items"]) / PAGER_SIZE);

	$command = strtolower(poll_fifo(FIFO_IREXEC));
	logger("Command: " . $command);

	// NUMBERS

	if($command==1 || $command==2 || $command==3 || $command==4) {
		// Establish Menu
		$confirmed = true;
		$index = $command + (($page - 1) * PAGER_SIZE) - 1;
		$item = $menu["items"][$index];
		if($item["confirm"] != "") {
			$data = "";
			$data .= render_lcd_row(1, CONFIRM_1);
                	$data .= render_lcd_row(2, CONFIRM_2);
                	$data .= render_lcd_row(3, CONFIRM_3);
                  	$data .= render_lcd_row(4, CONFIRM_4);
			refresh_lcd($data);
			$confirm = strtolower(poll_fifo(FIFO_IREXEC));
			if($confirm != "ok") { $confirmed = false; }
		}
		if($confirmed == true) {
			if($item["menu"] != "") {
				$new_menu = find_menu($menus, $item["menu"]);
				if($new_menu) { $menu = $new_menu; }
				logger("Jump to menu '" . $menu["id"] . "'");
			}
			// Execute Command
			if($item["command"] != "") {
				exec($item["command"]);
				logger("Launched '" . $item["command"] . "'");
			}
		}
	}

	// PAGE UP

	if($command == "up") {
		if($page > 1) { $page--; }
		logger("Page up to " . $page);
	}

	// PAGE DOWN

	if($command == "down") {
		if($page < $pages) { $page++; }
		logger("Page down to " . $page);
	}

	// HOME

	if($command == "home") {
		$menu = $menus[0];
		$page = 1;
		logger("Jump to home menu");
	}

	// BACK

	if($command == "back") {
		logger("Jump to parent menu '" . $menu["parent"] . "'");
		$menu = find_menu($menus, $menu["parent"]);
		$page = 1;
	}
	

}

// FUNCTIONS

function find_menu($menus, $menu_id) {
	foreach($menus as $menu_item) {
		if($menu_item["id"] == $menu_id) {
			$menu = $menu_item;
			break;
		}
	}
	return $menu;
}

function render_menu($menu, $page = 1) {
	logger("Rendering Menu", LOG_DEBUG);
	$data = "";
	$start = 1 + (($page -1) * PAGER_SIZE);
	for($i = $start; $i <= ($start + (PAGER_SIZE - 1)); $i++) {
		$item = $menu["items"][$i - 1];
		$key = $i - ($start - 1);
		if($item) {
			$line = $key . ": " . substr($item["label"], 0, 17);
			$data .= render_lcd_row($key, $line);
		}
	}
	//echo $data;
	return $data;
}

function logger($message, $level = LOG_INFO) {
	if($level <= LOG_LEVEL) {
		$output = "";
		if($level == LOG_ERROR) { $output .= "[ERROR] "; }
		if($level == LOG_INFO) { $output .= "[INFO]  "; }
		if($level == LOG_DEBUG) { $output .= "[DEBUG] "; }
		$output .= $message;
		echo $output . "\n";
	}
}

function poll_fifo($filename) {
	$handle = fopen($filename, "r");
	$contents = trim(fread($handle, 8192));
	fclose($handle);
	return $contents;
}

function render_lcd_row($row, $value)
{
        $data = "";
        $data .= "line" . $row . NL;
        $data .= "0" . NL;
        $data .= EOD . NL;
        $data .= $value . NL;
        $data .= EOD . NL;
        return $data;
}


function refresh_lcd($data)
{
	clear_lcd();
	send_lcd($data);
}

function clear_lcd()
{
	$cls = "";
	$cls .= render_lcd_row(1, "");
	$cls .= render_lcd_row(2, "");
	$cls .= render_lcd_row(3, "");
	$cls .= render_lcd_row(4, "");
	send_lcd($cls);
}

function send_lcd($data)
{
        $handle = fopen(FIFO_PERTD2, "w");
        fwrite($handle, $data);
        fclose($handle);
}


?>
